<?php

/**
 * login actions.
 *
 * @package    SCCEIC
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  		if($this->getUser()->isAuthenticated())
  			$this->redirect("votar/index");
  		$this->error = $request->getParameter("error",'0');  
  }
   public function executeDologin(sfWebRequest $request)
  {
  	$email = $request->getParameter('email');
  	$password = $request->getParameter('password');
  	
  	//Get user from password
  	$c = new Criteria();
  	$c->add(UsuarioPeer::MAIL,$email,Criteria::EQUAL);
  	$usuario = UsuarioPeer::doSelectOne($c);
  	if(is_null($usuario))
  		$this->redirect("login/index?error=1");
  	else if($usuario->getPassword()!=$password)
  		$this->redirect("login/index?error=2");
  	else
  	{
  		$sfU = $this->getUser();
  		$sfU->setAuthenticated(true);
		$sfU->setAttribute("email",$email);
		$sfU->setAttribute("from",$request->getReferer());
		if($usuario->getAdmin())
			$sfU->addCredential("admin");
  		$this->redirect($request->getReferer());
  	}  		
  }
  
  public function executeLogout(sfWebRequest $request)
  {
  	$this->getUser()->clearCredentials();		
	$this->getUser()->setAuthenticated(false);
	$this->getUser()->getAttributeHolder()->clear();
  	$this->redirect("login/index");
  } 
  
  public function executeRegistrar(sfWebRequest $request){
  	$this->form = new UsuarioForm();
  }  
  
  public function executeNotificar(sfWebRequest $request)
  {
	$mailBody = $this->getPartial('mail', array('name' => 'John Doe'));
	$mailTo = $request->getParameter('email');
	$mailFrom="MANGOSTA COMPANY";
	
	try
	{
	  // Create the mailer and message objects
	  $mailer = new Swift(new Swift_Connection_SMTP('smtp.ldc.usb.ve'));
	 // $mailer = new Swift(new Swift_Connection_NativeMail());
	  
	  $message = new Swift_Message('ENHORABUENA!!! ERES UN PAJUO!!', $mailBody, 'text/html');
	 
	  // Send
	  $mailer->send($message, $mailTo, $mailFrom);
	  $mailer->disconnect();
	  
	}
	catch (Exception $e)
	{
	  $mailer->disconnect();
	 
	  // handle errors here
	}	
  }

  
  
}
